package com.weige.conver.utils;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtils {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 设置过期时间
     * @param key
     * @param object
     * @return
     */
    public boolean setByOverdue(String key,Object object,int time){
        boolean result = false;
        try {
            redisTemplate.opsForValue().set(key, JSONObject.toJSONString(object),time, TimeUnit.MINUTES);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 读取缓存
     *
     * @param key
     * @return
     */
    public String get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    /**
     * 写入缓存
     */
    public boolean set(String key, String value) {
        boolean result = false;
        try {
            redisTemplate.opsForValue().set(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 更新缓存
     */
    public boolean getAndSet(String key, String value) {
        boolean result = false;
        try {
            redisTemplate.opsForValue().getAndSet(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 删除缓存
     */
    public boolean delete(String key) {
        boolean result = false;
        try {
            redisTemplate.delete(key);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public boolean setByList(String key,String data){
        boolean result = false;
        try {
            redisTemplate.opsForList().leftPush(key,data);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public List getByList(String key){
        return redisTemplate.opsForList().range(key,0,-1);
    }

    public long getByListLength(String key){
        return redisTemplate.opsForList().size(key);
    }

    public boolean deleteByList(String key) {
        boolean result = false;
        try {
            redisTemplate.opsForList().rightPop(key);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public boolean setBySet(String key,String data){
        boolean result = false;
        try {
            redisTemplate.opsForSet().add(key,data);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public Set getBySet(String key){
        return redisTemplate.opsForSet().members(key);
    }

    public boolean setByHash(String key,String hashKey,Object data){
        boolean result = false;
        try {
            redisTemplate.opsForHash().put(key,hashKey,data);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public List getByHash(String key){
        return redisTemplate.opsForHash().values(key);
    }

    public Object getKeyByHash(String key,String hashKey){
        return redisTemplate.opsForHash().get(key,hashKey);
    }


    public boolean deleteKey(String key) {
        boolean result = false;
        try {
            redisTemplate.delete(key);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
